package com.swz.dcrm.adpter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.billy.android.loading.Gloading;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.swz.dcrm.R;
import com.swz.dcrm.util.Tool;
import com.wang.avi.AVLoadingIndicatorView;
import com.wang.avi.Indicator;

import java.util.zip.Inflater;


/**
 * @author xh on 2019/5/10
 */
public class GlobalAdapter implements Gloading.Adapter {

    public static final int NET_ERROR=1000;
    public static final int NET_ERROR_NOT_NEED_MARGIN=1001;
    public static final int LOADING_NOT_NEED_MARGIN=1002;
    @Override
    public View getView(Gloading.Holder holder, View convertView, int status) {

        GlobalLoadingStatusView loadingStatusView = null;
        //reuse the old view, if possible
        if (convertView != null && convertView instanceof GlobalLoadingStatusView) {
            loadingStatusView = (GlobalLoadingStatusView) convertView;
        }
        if (loadingStatusView == null) {
            loadingStatusView = new GlobalLoadingStatusView(holder.getContext(), holder.getRetryTask());
        }

        loadingStatusView.setStatus(status);
        return loadingStatusView;
    }



    class GlobalLoadingStatusView extends RelativeLayout {


        ConstraintLayout cNetError;
        ImageView loading;
        View view;

        public GlobalLoadingStatusView(Context context, Runnable retryTask) {
            super(context);
            //init view ...
            view=  LayoutInflater.from(context).inflate(R.layout.layout_data_state,this,false);
            cNetError=view.findViewById(R.id.c_net_error);
            loading=view.findViewById(R.id.loading);
            Glide.with(context)
                    .load(R.mipmap.loading)
                    .into(loading);
            cNetError.setOnClickListener(v -> {
                retryTask.run();
            });
            LayoutParams lp = new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);


            addView(view,lp);
        }

        public void setStatus(int status) {
            //change ui by different status...
            LayoutParams lp= (LayoutParams) view.getLayoutParams();

            switch (status){
                case Gloading.STATUS_EMPTY_DATA:
                    break;
                case NET_ERROR:
                    lp.setMargins(0, getStatusBarHeight(getContext())+Tool.dip2px(getContext(),50),0,0);
                    view.setLayoutParams(lp);
                    loading.setVisibility(VISIBLE);
                    setVisibility(GONE);
                    cNetError.setVisibility(VISIBLE);
                    break;
                case Gloading.STATUS_LOAD_SUCCESS:
                    setVisibility(GONE);
                    cNetError.setVisibility(GONE);
                    loading.setVisibility(GONE);
                    break;
                case Gloading.STATUS_LOADING:
                    lp.setMargins(0, getStatusBarHeight(getContext())+Tool.dip2px(getContext(),50),0,0);
                    view.setLayoutParams(lp);
                    setVisibility(VISIBLE);
                    loading.setVisibility(VISIBLE);
                    cNetError.setVisibility(GONE);
                    break;
                case NET_ERROR_NOT_NEED_MARGIN:
                    loading.setVisibility(VISIBLE);
                    setVisibility(GONE);
                    cNetError.setVisibility(VISIBLE);
                    lp.setMargins(0,0,0,0);
                    view.setLayoutParams(lp);
                    break;
                case LOADING_NOT_NEED_MARGIN:
                    setVisibility(VISIBLE);
                    loading.setVisibility(VISIBLE);
                    cNetError.setVisibility(GONE);
                    lp.setMargins(0,0,0,0);
                    view.setLayoutParams(lp);
                    break;

            }
        }
    }
    public   int getStatusBarHeight(Context context) {
        int result = 0;
        int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resId > 0) {
            result = context.getResources().getDimensionPixelOffset(resId);
        }
        return result;
    }
}
